package com.dq.controller.app.goods;

import com.alibaba.fastjson.JSONObject;
import com.dq.controller.api.small.HotSearchGoodsController;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.controller.in.goods.BossGoodsQuery;
import com.dq.controller.in.goods.stock.OrgGoodsAndStockQuery;
import com.dq.controller.in.query.GoodsQuery;
import com.dq.controller.in.query.IntegralGoodsQuery;
import com.dq.domain.annex.AnnexTypeEnum;
import com.dq.domain.annex.MallAnnex;
import com.dq.domain.goods.GoodSpecification;
import com.dq.domain.goods.Goods;
import com.dq.domain.goods.Product;
import com.dq.domain.sys.user.UserCollection;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.exception.customException.AdminException;
import com.dq.exception.customException.ApiException;
import com.dq.service.AllService;
import com.dq.utils.SmallQrCode;
import com.dq.vo.ListDataVo;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author liuhui
 * @date 2019/9/16 0016 13:47:38
 */
@Api(description = "商品管理")
@RestController
@RequestMapping("app/goods/")
@Log
public class AppGoodsController extends AllService {

    @PostMapping("orgListAll")
    @ApiOperation("门店商品列表, 无库存: [出库登记]")
    public Vo orgListAll(@RequestBody OrgGoodsAndStockQuery orgGoodsAndStockQuery) throws AdminException {
        if (orgGoodsAndStockQuery.getGoodsStatus() == null) {
            orgGoodsAndStockQuery.setGoodsStatus(Goods.GoodsStatus.upperShelf);
        }
        orgGoodsAndStockQuery.format();
        orgGoodsAndStockQuery.setPage(null);
        orgGoodsAndStockQuery.setSize(null);
        // 关联门店, 门店ID, 不传默认是登录账号门店下
        long orgId = orgGoodsAndStockQuery.getOrgId() == null ? sysUserService.getUserInSection().getSection().getId() : orgGoodsAndStockQuery.getOrgId();

        List<Goods> goodsList = goodsService.goodsByOrgList(orgGoodsAndStockQuery, orgId);

        return new ListDataVo((List<Goods.Vo>) VO.toVOCollection(goodsList));
    }

    @ApiOperation("所有商品列表-不关联门店, 无库存: [入库登记]")
    @PostMapping("listAll")
    public Vo listAll(@RequestBody BossGoodsQuery bossGoodsQuery) throws AdminException {
        bossGoodsQuery.format();
        bossGoodsQuery.setPage(null);
        bossGoodsQuery.setSize(null);
        if(bossGoodsQuery.getGoodsStatus() == null) {
            bossGoodsQuery.setGoodsStatus(Goods.GoodsStatus.upperShelf);
        }
        List<Goods> goodsList = goodsService.list(bossGoodsQuery);
        return new ListDataVo((List<Goods.Vo>) VO.toVOCollection(goodsList));
    }

    @ApiOperation("获取单个商品goods库存")
    @PostMapping("getGoodsStock")
    public Vo getGoodsStock(@ApiParam("只需goodsId参数, orgId不传默认根据登录用户") @RequestBody OrgGoodsAndStockQuery orgGoodsAndStockQuery) throws AdminException {
        if (orgGoodsAndStockQuery.getGoodsId() == null) {
            throw new AdminException("商品ID为空");
        }
        // 关联门店, 门店ID, 不传默认是登录账号门店下
        long orgId = orgGoodsAndStockQuery.getOrgId() == null ? sysUserService.getUserInSection().getSection().getId() : orgGoodsAndStockQuery.getOrgId();
        return new Vo(goodsService.goodsStock(orgGoodsAndStockQuery.getGoodsId(), orgId));
    }

    @GetMapping("info/{goodsId}")
    @ApiOperation("商品详情检索")
    public Vo<Goods.Vo> infoQuery(@PathVariable @ApiParam("商品id") Long goodsId) throws AdminException {
        // 商品基本信息
        Goods goods = dao.get(Goods.class, goodsId);
        if (null == goods) {
            throw new AdminException("商品不存在");
        }
        //商品轮播图
        List<MallAnnex> mallAnnexes = annexRepository.findByAnnexTypeEnumAndGoodsAndDeletedFalse(AnnexTypeEnum.goodBanner, goods);
        List<String> imgs = new ArrayList<>();
        mallAnnexes.forEach(mallAnnex -> {
            imgs.add(mallAnnex.getShowUrl());
        });

        Goods.Vo goodVo = (Goods.Vo) goods.toVO(false);
        return new Vo(goodVo);
    }

}
